import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:nft_shop/my/model/models.dart';
import 'package:nft_shop/net/service_repository.dart';
import 'package:nft_shop/res/constant.dart';
import 'package:nft_shop/res/global.dart';
import 'package:nft_shop/widgets/load_image.dart';
import 'package:nft_shop/widgets/my_app_bar.dart';
import 'package:nft_shop/widgets/my_scroll_view.dart';
import 'package:nft_shop/widgets/status_view.dart';

class MyContactUsPage extends StatefulWidget {
  const MyContactUsPage({Key? key}) : super(key: key);

  @override
  _MyContactUsPageState createState() => _MyContactUsPageState();
}

class _MyContactUsPageState extends State<MyContactUsPage> {
  ConfigNameDetail? model;

  ///状态
  int _status = LoadStatusType.loading;

  int get status => _status;

  set status(int value) {
    _status = value;
    setState(() {

    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getData();
  }

  getData()async{
    try{
      List list = await ServiceRepository.getConfigNameDetail("connect_us");
      if(list.length != 0){
        list.forEach((element) {
          if(element.name == "connect_us"){
            model = element;
          }
        });
      }
      if(model != null){
        status = LoadStatusType.success;
      }else{
        status = LoadStatusType.empty;
      }
    }catch(e,s){
      print("错误信息----${e}--${s}");
      status = LoadStatusType.empty;
    }
  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: MyAppBar(
        centerTitle: "联系我们",
      ),
      body: Builder(
        builder: (context) {
          if(status != LoadStatusType.success){
            return StatusViews(status);
          }

          ConnectUsModel? connect = JsonUtil.getObj(model!.value, (v) => ConnectUsModel.fromJson(v));

          return MyScrollView(
            children: [
              _contactUsItem(
                height: 80,
                title: "官网",
                content: connect!.contGw??"",
              ),
              _contactUsItem(
                  height: 80,
                  title: "微信号",
                  content:connect.contWx??"",
                  isCopy: true,
                  onTap: () {
                    if (!ObjectUtil.isEmptyString(connect.contWx)) {
                      ClipboardData data = new ClipboardData(
                          text: connect.contWx);
                      Clipboard.setData(data);
                      EasyLoading.showToast("复制成功");
                    }
                  }),
              _contactUsItem(
                height: 80,
                title: "邮箱",
                content: connect.contYx??"",
              ),
            ],
          );
        }
      ),
    );
  }

  Widget _contactUsItem(
      {required double height,
        required String title,
        required String content,
        bool isCopy = false,
        GestureTapCallback? onTap}) {
    return Container(
      // height: height,
      margin: const EdgeInsets.only(left: 15.0),
      width: double.infinity,
      decoration: BoxDecoration(
        border: Border(
          bottom: Divider.createBorderSide(context,
              width: 1, color: Global.kTheme.colorLine),
        ),
      ),
      padding: EdgeInsets.only(top: 20, right: 15, bottom: 20),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(title,style: TextStyle(fontSize: 14,fontFamily: Global.myRegularFont,color: Global.kTheme.textPrimary7),),
          SizedBox(
            height: 7,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Text(
                content,
                style: TextStyle(
                    fontSize: 14,
                    fontFamily: Global.mySemiboldFont,
                    color: Global.kTheme.textPrimary6,),
              ),
              SizedBox(
                width: 5,
              ),
              Visibility(
                visible: isCopy,
                child: InkWell(
                  child: LoadAssetImage(
                    "my_copy",
                    width: 14,
                    fit: BoxFit.fitWidth,
                  ),
                  onTap: onTap,
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
